﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using FAQFrameworks;
using System.Collections;
using FooFun.DataElements.Dtos;

namespace FooFun.Web.Controls
{
    public class ReplysViewControl : DataElementControl
    {
        public override IDataElement DataElement
        {
            get
            {
                return base.DataElement;
            }
            set
            {

                base.DataElement = new ReplysDataElement(value);
            }
        }
    }

    public class ReplysDataElement : CustomDataElement
    {
        IDataElement topAnswer;
        IDataElement selfAnswer;

        SortedDictionary<object, IDataElement> sortedDic;
        List<IDataElement> list;

        public ReplysDataElement(IDataElement replys)
            : base(replys)
        {
            sortedDic
                = new SortedDictionary<object, IDataElement>();

            if (!replys.IsEmpty)
            {
                // todo: 重新实现
                IDataElement de = replys["Content"];
                object topUser = null;
                object selfUser = null;

                foreach (IDataElement contentDE in de)
                {
                    EventManager eventManager = new EventManager();
                    IDataElement eventDE
                        = eventManager.GetElement(
                        contentDE["@EventId"]);

                    if (contentDE["@EventId"].Equals
                        (replys["@Top"]))
                    {
                        topAnswer = contentDE;
                        topUser = eventDE["AccountId"].Value;

                        continue;
                    }

                    if (eventDE["AccountId"].Equals
                        (FAQContext.Current.User.AccountId))
                    {
                        selfAnswer = contentDE;
                        selfUser = eventDE["AccountId"].Value;

                        continue;
                    }

                    sortedDic[eventDE["AccountId"].Value] = contentDE;

                    //list.Add(contentDE);
                }

                if (topUser != null)
                {
                    sortedDic.Remove(topUser);
                    if (topUser.Equals(selfUser))
                    {
                        selfAnswer = null;
                    }
                }

                list
                    = new List<IDataElement>(sortedDic.Values);

                list.Sort(new ReplysComparer());
            }

            if (selfAnswer == null)
            {
                selfAnswer = new EmptyDataElement("没有自己的答案", replys);
            }
            if (topAnswer == null)
            {
                topAnswer = new EmptyDataElement("没有标准答案", replys);
            }
        }


        public override IDataElement this[string attrName]
        {
            get
            {
                if (attrName == "TopAnswer")
                {
                    return topAnswer;
                }
                else if (attrName == "SelfAnswer")
                {
                    return selfAnswer;
                }
                else if (attrName == "OtherAnswers")
                {
                    return new ArrayDataElement(list.ToArray());
                }
                return base[attrName];
            }
            set
            {
                base[attrName] = value;
            }
        }

        class ReplysComparer : IComparer<IDataElement>
        {
            public ReplysComparer()
            {
            }

            #region IComparer<IDataElement> 成员

            public int Compare(IDataElement x, IDataElement y)
            {
                int xId = 0;
                int yId = 0;

                int.TryParse(x["@EventId"].ToString(), out xId);
                int.TryParse(y["@EventId"].ToString(), out yId);

                return yId - xId;
            }

            #endregion
        }
    }
}
